package main

import (
	"github.com/gin-gonic/gin"
	"github.com/jonasngs/go_entry_task/http_server/config"
	"github.com/jonasngs/go_entry_task/http_server/global"
	"github.com/jonasngs/go_entry_task/http_server/user"
	"github.com/jonasngs/go_entry_task/http_server/user/impl"
	"github.com/jonasngs/go_entry_task/protobuf3/pb"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
)

func main() {
	initBaseServer()
	router := gin.Default()
	router.GET("/profile", user.RefInsuranceManager().ProfileHandler)
	router.POST("/login", user.RefInsuranceManager().LoginRequestHandler)
	router.GET("/edit", user.RefInsuranceManager().UpdateHandler)
	router.POST("/edit", user.RefInsuranceManager().UpdateRequestHandler)
	router.Static("/resources", "../resources") // Static route for profile picture
	router.Run()                                // Using Default port 8080
}

func initBaseServer() {

	config.OpenConfigFile()

	var opts []grpc.DialOption
	opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
	conn, err := grpc.Dial(config.GetTCPServer(), opts...)
	if err != nil {
		log.Fatalf("fail to dial: %v", err)
	}
	//defer conn.Close()
	global.GrpcUserManagerClient = pb.NewUserManagerClient(conn)

	user.InjectManager(new(impl.ManagementImpl))
}
